package com.linck.im.client.handler;

import com.linck.im.client.context.AccountContext;
import com.linck.im.client.context.ChatContext;
import com.linck.im.client.controller.ChatController;
import com.linck.im.model.entity.Account;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 好友列表处理器
 *
 * @author: linck
 * @create: 2021-01-26
 */
public class ClientFriendsHandler extends SimpleChannelInboundHandler<ProtoMsg.Friends> {

    private static final Logger log = LoggerFactory.getLogger(ClientFriendsHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtoMsg.Friends friends) throws Exception {
        log.info("好友列表 \n{}", friends.getFriendList());
        AccountContext.accountList = new ArrayList<>();
        friends.getFriendList().forEach(friend -> {
            Account account = new Account();
            account.setAccount(friend.getAccount());
            AccountContext.accountList.add(account);
        });
        List<String> accounts = friends.getFriendList().stream().map(t -> t.getAccount()).collect(Collectors.toList());
        ChatController.setFriendListView(accounts);
        ChatContext.init(accounts);
    }
}
